package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthServices;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.model.business.Health;
import com.goods.common.model.business.Supplier;
import com.goods.common.model.system.User;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/10  14:00
 * @description：TODO
 */
@Service
public class HealthServicesImpl implements HealthServices {
    @Autowired(required = false)
    private HealthMapper healthMapper;


    //签到查询
    @Override
    public ResponseBean isReport(Long id) {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String date = dft.format(new Date());
        Example o = new Example(Health.class);
        Example.Criteria criteria = o.createCriteria();
        criteria.andEqualTo("userId",id);
        criteria.andLike("createTime","%"+date+"%");
        Health health = healthMapper.selectOneByExample(o);
        if(health!=null){
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(health,healthVO);
            return ResponseBean.success(healthVO);
        }
        return ResponseBean.success(null);
    }


    //进行打卡
    @Override
    public void report(HealthVO healthVO, Long id) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        health.setUserId(id);
        health.setCreateTime(new Date());
        healthMapper.insert(health);
    }

    //查询历史记录
    @Override
    public PageVO<HealthVO> history(Integer pageNum, Integer pageSize, HealthVO healthVO) {

        PageHelper.startPage(pageNum,pageSize);


        List<Health> healthList = healthMapper.selectAll();
        List<HealthVO>healthVoList=new ArrayList<>();
        for (Health health : healthList) {
            HealthVO healthVO1 = new HealthVO();
            BeanUtils.copyProperties(health,healthVO1);
            healthVoList.add(healthVO1);
        }
        PageInfo<Supplier> info=new PageInfo(healthList);
        return new PageVO<>(info.getTotal(),healthVoList);
    }


}
